import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAreaService extends BaseService<any> {
  private readonly logger = new Logger(CoreAreaService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getInfo
   * 对应 PHP: CoreAreaService_core::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: any) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: return $this->model->where('id', $id)->field('id, pid, name, shortname, longitude, latitude, level, sort, status')->find()->toArray();     }      /** ...
return this.model.where('id', id).field('id, pid, name, shortname, longitude, latitude, level, sort, status').find().toArray(];
    }

    /**
     * 获取地址列表
     * @param int pid
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getListByPid(int pid)
    {
        return this.model.where('pid', pid).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
    }

    /**
     * 获取地址的树形结构
     * @param int level
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getTree(int level = 3)
    {
        const list = this.model.where('level', '<=', level).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
        return list_to_tree(list);
    }

    /**
     * 通过对应省市区县，地址，返回完整地址
     * @param province_id
     * @param city_id
     * @param district_id
     * @param address
     * @param string tag 分隔符
     * @return string
     */
    async getFullAddress(province_id, city_id, district_id, address, tag = ' ')
    {
        const province_name = (this.model.where([['id', '=', province_id))).field("name").findOrEmpty().toArray()).name || '';
        const city_name = (this.model.where([['id', '=', city_id))).field("name").findOrEmpty().toArray()).name || '';
        const district_name = (this.model.where([['id', '=', district_id))).field("name").findOrEmpty().toArray()).name || '';
        return province_name.tag.city_name.tag. district_name. tag. address;
    }
}
  }

  /**
   * getListByPid
   * 对应 PHP: CoreAreaService_core::getListByPid()
   * 逻辑类型: undefined - undefined
   */
  async getListByPid(pid: number) {
    // 基于PHP真实逻辑: getListByPid
    // PHP原文: return $this->model->where('pid', $pid)->field('id, pid, name, shortname, longitude, latitude, level, sort, status')->select()->toArray();     }      ...
return this.model.where('pid', pid).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
    }

    /**
     * 获取地址的树形结构
     * @param int level
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getTree(int level = 3)
    {
        const list = this.model.where('level', '<=', level).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
        return list_to_tree(list);
    }

    /**
     * 通过对应省市区县，地址，返回完整地址
     * @param province_id
     * @param city_id
     * @param district_id
     * @param address
     * @param string tag 分隔符
     * @return string
     */
    async getFullAddress(province_id, city_id, district_id, address, tag = ' ')
    {
        const province_name = (this.model.where([['id', '=', province_id))).field("name").findOrEmpty().toArray()).name || '';
        const city_name = (this.model.where([['id', '=', city_id))).field("name").findOrEmpty().toArray()).name || '';
        const district_name = (this.model.where([['id', '=', district_id))).field("name").findOrEmpty().toArray()).name || '';
        return province_name.tag.city_name.tag. district_name. tag. address;
    }
}
  }

  /**
   * getTree
   * 对应 PHP: CoreAreaService_core::getTree()
   * 逻辑类型: undefined - undefined
   */
  async getTree(level: number) {
    // 基于PHP真实逻辑: getTree
    // PHP原文: $list = $this->model->where('level', '<=', $level)->field('id, pid, name, shortname, longitude, latitude, level, sort, status')->select()->toArray(); ...
const list = this.model.where('level', '<=', level).field('id, pid, name, shortname, longitude, latitude, level, sort, status').select().toArray(];
        return list_to_tree(list);
    }

    /**
     * 通过对应省市区县，地址，返回完整地址
     * @param province_id
     * @param city_id
     * @param district_id
     * @param address
     * @param string tag 分隔符
     * @return string
     */
    async getFullAddress(province_id, city_id, district_id, address, tag = ' ')
    {
        const province_name = (this.model.where([['id', '=', province_id))).field("name").findOrEmpty().toArray()).name || '';
        const city_name = (this.model.where([['id', '=', city_id))).field("name").findOrEmpty().toArray()).name || '';
        const district_name = (this.model.where([['id', '=', district_id))).field("name").findOrEmpty().toArray()).name || '';
        return province_name.tag.city_name.tag. district_name. tag. address;
    }
}
  }

  /**
   * getFullAddress
   * 对应 PHP: CoreAreaService_core::getFullAddress()
   * 逻辑类型: undefined - undefined
   */
  async getFullAddress(province_id: string, city_id: string, district_id: string, address: string, tag: string) {
    // 基于PHP真实逻辑: getFullAddress
    // PHP原文: $province_name = ($this->model->where([['id', '=', $province_id]])->field("name")->findOrEmpty()->toArray())['name'] ?? '';         $city_name = ($thi...
const province_name = (this.model.where([['id', '=', province_id))).field("name").findOrEmpty().toArray()).name || '';
        const city_name = (this.model.where([['id', '=', city_id))).field("name").findOrEmpty().toArray()).name || '';
        const district_name = (this.model.where([['id', '=', district_id))).field("name").findOrEmpty().toArray()).name || '';
        return province_name.tag.city_name.tag. district_name. tag. address;
    }
}
  }
}
